package com.campus.handler;

import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

@Component
@Slf4j
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.error("权限错误：{}",accessDeniedException.getMessage(),accessDeniedException);
        response.setContentType("application/json;charset=UTF-8");
        ResponseResult<Object> result = new ResponseResult<>(ResultCode.NO_ACCESS,null);
        String json = Objects.toString(result);
        response.getWriter().write(json);
    }
}
